﻿using System.Collections.Generic;
using System.Linq;
using PetaPoco;

namespace ZhongheCms.Models
{
    public partial class GswTblModuleText
    {
        [ResultColumn]
        public List<GswTblFunction> Funcions { get; set; }

        public static List<GswTblModuleText> GetModuleList()
        {

            var moduleList = Fetch("where ShowFlag=0 order by ModuleNo");
            foreach (var m in moduleList)
                m.Funcions = GswTblFunction.Fetch("where ModuleId=@0 and ShowFlag=0 order by FuncNo", m.Id);

            return moduleList;
        }
        public static List<GswTblModuleText> GetModuleList(int userId)
        {

            var userPower = GswTblUserPower.Fetch("where UserId=@0", userId);
            var moduleList = (from m in userPower select m.ModuleId).Distinct();
            string moduleIds = "";
            foreach (var s in moduleList)
            {
                if (string.IsNullOrEmpty(moduleIds))
                    moduleIds = s.ToString();
                else
                    moduleIds += "," + s;
            }
            string sql = "";
            if (moduleIds != "")
                sql = "where id in (" + moduleIds + ") order by ModuleNo";
            else
                sql = "1=0";
            var moduleTexts = Fetch(sql);
            foreach (var m in moduleTexts)
            {
                var moduleId = m.Id;
                var funcList = from f in userPower where f.ModuleId == moduleId select f.FunId;
                string fids = "";
                foreach (var f in funcList)
                {
                    if (string.IsNullOrEmpty(fids))
                        fids = f.ToString();
                    else
                        fids += "," + f.ToString();
                }
                m.Funcions = GswTblFunction.Fetch("where ShowFlag=0 and Id in (" + fids + " ) order by FuncNo ");
            }
            return moduleTexts;


        }
    }
}